package uno.linze.interview_question.util;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    /**
     * 发送简单文本邮件
     * @param to 接收方邮箱
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendSimpleEmail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
        System.out.println("简单文本邮件发送成功！");
    }

    /**
     * 发送HTML邮件
     * @param to 接收方邮箱
     * @param subject 邮件主题
     * @param htmlContent HTML内容
     * @throws MessagingException
     */
    public void sendHtmlEmail(String to, String subject, String htmlContent) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(fromEmail);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(htmlContent, true); // true表示内容为HTML
        mailSender.send(message);
        System.out.println("HTML邮件发送成功！");
    }

    /**
     * 发送带附件的邮件
     * @param to 接收方邮箱
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param filePath 附件文件路径
     * @throws MessagingException
     */
    public void sendEmailWithAttachment(String to, String subject, String content, String filePath) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true); // true表示支持多部分邮件
        helper.setFrom(fromEmail);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content);

        FileSystemResource file = new FileSystemResource(new File(filePath));
        helper.addAttachment(file.getFilename(), file); // 添加附件
        mailSender.send(message);
        System.out.println("带附件邮件发送成功！");
    }
    /**
     * 发送验证码邮件
     * @param email 接收方邮箱
     * @param verificationCode 6位验证码
     * @throws MessagingException
     */
    public void sendVerificationCodeEmail(String email, String verificationCode) throws MessagingException {
        String subject = "您的 LinZe 验证码";

        // Corrected the HTML content by escaping literal '%' with '%%'
        String htmlContent = """
                <!doctype html>
                <html lang="und" dir="auto" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
                <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                    <title>LinZe Verifaction Code</title>
                    <meta http-equiv="X-UA-Compatible" content="IE=edge">
                    <meta name="viewport" content="width=device-width, initial-scale=1">
                    <style type="text/css">
                        #outlook a {
                            padding: 0;
                        }
                
                        body {
                            margin: 0;
                            padding: 0;
                            -webkit-text-size-adjust: 100%%;
                            -ms-text-size-adjust: 100%%;
                        }
                
                        table,
                        td {
                            border-collapse: collapse;
                            mso-table-lspace: 0pt;
                            mso-table-rspace: 0pt;
                        }
                
                        img {
                            border: 0;
                            height: auto;
                            line-height: 100%%;
                            outline: none;
                            text-decoration: none;
                            -ms-interpolation-mode: bicubic;
                        }
                
                        p {
                            display: block;
                            margin: 13px 0;
                        }
                    </style>
                    <link href="https://fonts.googleapis.com/css?family=Noto+Sans+SC:100,300,400,500,700,900|Noto+Serif+SC:200,300,400,500,600,700,900" rel="stylesheet" type="text/css">
                    <style type="text/css">
                        @import url(https://fonts.googleapis.com/css?family=Noto+Sans+SC:100,300,400,500,700,900|Noto+Serif+SC:200,300,400,500,600,700,900);
                    </style>
                    <style type="text/css">
                        @media only screen and (min-width:480px) {
                            .mj-column-per-100 {
                                width: 100%% !important;
                                max-width: 100%%;
                            }
                
                            .mj-column-px-640 {
                                width: 640px !important;
                                max-width: 640px;
                            }
                        }
                    </style>
                    <style media="screen and (min-width:480px)">
                        .moz-text-html .mj-column-per-100 {
                            width: 100%% !important;
                            max-width: 100%%;
                        }
                
                        .moz-text-html .mj-column-px-640 {
                            width: 640px !important;
                            max-width: 640px;
                        }
                    </style>
                    <style type="text/css">
                        @media only screen and (max-width:479px) {
                            table.mj-full-width-mobile {
                                width: 100%% !important;
                            }
                
                            td.mj-full-width-mobile {
                                width: auto !important;
                            }
                        }
                    </style>
                    <style type="text/css">
                        @media all and (max-width: 600px) {
                            .code-text div {
                                font-size: 50px !important;
                                line-height: 50px !important;
                            }
                        }
                
                        @media all and (max-width: 400px) {
                            .code-text div {
                                font-size: 30px !important;
                                line-height: 30px !important;
                            }
                        }
                    </style>
                </head>
                
                <body style="word-spacing:normal;background-color:#F6F7FA;">
                    <div style="background-color:#F6F7FA;" lang="und" dir="auto">
                        <div style="margin:0px auto;max-width:900px;">
                            <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%%;">
                                <tbody>
                                    <tr>
                                        <td style="direction:ltr;font-size:0px;padding:0px;padding-top:46px;text-align:center;">
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        <div style="background:#FFFFFF;background-color:#FFFFFF;margin:0px auto;max-width:900px;">
                            <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#FFFFFF;background-color:#FFFFFF;width:100%%;">
                                <tbody>
                                    <tr>
                                        <td style="direction:ltr;font-size:0px;padding:0px;padding-left:10px;padding-right:10px;padding-top:65px;text-align:center;">
                                            <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%%;">
                                                <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%%">
                                                    <tbody>
                                                        <tr>
                                                            <td align="center" style="font-size:0px;padding:0px;word-break:break-word;">
                                                                <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px;">
                                                                    <tbody>
                                                                        <tr>
                                                                            <td style="width:312px;">
                                                                                <img alt="" src="https://youke1.picui.cn/s1/2025/09/24/68d390547c678.png" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%%;font-size:13px;" width="312" height="auto">
                                                                            </td>
                                                                        </tr>
                                                                    </tbody>
                                                                </table>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        <div style="background:#ffffff;background-color:#ffffff;margin:0px auto;max-width:900px;">
                            <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%%;">
                                <tbody>
                                    <tr>
                                        <td style="direction:ltr;font-size:0px;padding:0px;padding-top:40px;text-align:center;">
                                            <div class="mj-column-px-640 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%%;">
                                                <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%%">
                                                    <tbody>
                                                        <tr>
                                                            <td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
                                                                <p style="border-top:solid 1px #DFE3E8;font-size:1px;margin:0px auto;width:100%%;"></p>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        <div style="background:#FFFFFF;background-color:#FFFFFF;margin:0px auto;max-width:900px;">
                            <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#FFFFFF;background-color:#FFFFFF;width:100%%;">
                                <tbody>
                                    <tr>
                                        <td style="direction:ltr;font-size:0px;padding:0px;padding-top:68px;text-align:center;">
                                            <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%%;">
                                                <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%%">
                                                    <tbody>
                                                        <tr>
                                                            <td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
                                                                <div style="font-family:Noto Sans SC, Arial, sans-serif;font-size:16px;font-weight:700;letter-spacing:0;line-height:23px;text-align:center;color:#020E36;">你好，%s</div>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        <div style="margin:0px auto;max-width:900px;">
                            <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%%;">
                                <tbody>
                                    <tr>
                                        <td style="direction:ltr;font-size:0px;padding:0px;text-align:center;">
                                            <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%%;">
                                                <table border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%%">
                                                    <tbody>
                                                        <tr>
                                                            <td style="background-color:#FFFFFF;vertical-align:top;padding-top:18px;">
                                                                <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="" width="100%%">
                                                                    <tbody>
                                                                        <tr>
                                                                            <td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
                                                                                <div style="font-family:Noto Sans SC, Arial, sans-serif;font-size:14px;font-weight:400;letter-spacing:0;line-height:20px;text-align:center;color:#020E36;">你的邮箱验证码为：</div>
                                                                            </td>
                                                                        </tr>
                                                                    </tbody>
                                                                </table>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        <div style="margin:0px auto;max-width:900px;">
                            <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%%;">
                                <tbody>
                                    <tr>
                                        <td style="direction:ltr;font-size:0px;padding:0px;text-align:center;">
                                            <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%%;">
                                                <table border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%%">
                                                    <tbody>
                                                        <tr>
                                                            <td style="background-color:#FFFFFF;vertical-align:top;padding-top:28px;">
                                                                <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="" width="100%%">
                                                                    <tbody>
                                                                        <tr>
                                                                            <td align="center" class="code-text" style="font-size:0px;padding:10px 25px;word-break:break-word;">
                                                                                <div style="font-family:Noto Sans SC, Arial, sans-serif;font-size:76px;font-weight:700;letter-spacing:0;line-height:110px;text-align:center;color:rgba(2, 14, 54, 0.8);">%s</div>
                                                                            </td>
                                                                        </tr>
                                                                    </tbody>
                                                                </table>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        <div style="margin:0px auto;max-width:900px;">
                            <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%%;">
                                <tbody>
                                    <tr>
                                        <td style="direction:ltr;font-size:0px;padding:0px;text-align:center;">
                                            <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%%;">
                                                <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="background-color:#FFFFFF;vertical-align:top;" width="100%%">
                                                    <tbody>
                                                        <tr>
                                                            <td align="center" style="font-size:0px;padding:10px 25px 50px;word-break:break-word;">
                                                                <div style="font-family:Noto Sans SC, Arial, sans-serif;font-size:12px;font-weight:400;letter-spacing:0;line-height:17px;text-align:center;color:rgba(2, 14, 54, 0.6);">5天内有效，请勿向他人泄露</div>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        <div style="margin:0px auto;max-width:900px;">
                            <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%%;">
                                <tbody>
                                    <tr>
                                        <td style="direction:ltr;font-size:0px;padding:0px;padding-top:46px;text-align:center;">
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </body>
                </html>
                """;

        // 格式化HTML内容，替换占位符
        String formattedHtml = String.format(htmlContent, email, verificationCode);

        // 调用现有的sendHtmlEmail方法发送邮件
        this.sendHtmlEmail(email, subject, formattedHtml);
        System.out.println("验证码邮件发送成功至：" + email);
    }
}
